package controller;

import java.util.concurrent.*;
import java.util.function.BiConsumer;
import java.util.function.Function;

public class SupplyAsyncDemo {

    public static void main(String[] args) throws ExecutionException, InterruptedException {

        final ExecutorService executorService = Executors.newFixedThreadPool(100);

        final CompletableFuture<Integer> future = CompletableFuture.supplyAsync(() -> {

            try {
                TimeUnit.SECONDS.sleep(3);
                System.out.println(111);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }

            return 123;
        }, executorService);

       /* //根据异步任务结果在做后续操作
        future.whenCompleteAsync(new BiConsumer<Integer, Throwable>() {
            @Override
            public void accept(Integer integer, Throwable throwable) {
                System.out.println("异步处理线程"+Thread.currentThread().getName());
                System.out.println(integer+123);
            }
        });*/

        future.exceptionally(new Function<Throwable, Integer>() {
            @Override
            public Integer apply(Throwable throwable) {
                System.out.println("异常线程"+Thread.currentThread().getName());
                //获取异常信息
                System.out.println(throwable.getMessage());
                return null;
            }
        });

    }

}
